package com.correction.servlet;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Part;

import com.correction.classes.Exercice.Langage;
import com.correction.utils.Tools;

@WebServlet("/upload")
@MultipartConfig
public class UploadService {
	private final Logger LOGGER = Logger.getLogger(Controler.class
			.getCanonicalName());
	private Part filePart = null;
	private String fileName = null;
	private PrintWriter writer = null;
	private InputStream filecontent = null;
	private String content = "";
	private final String path = "C:/FiletmpSave";

	// private String direction = "";

	public void setContent(String content) {
		this.content = content;
	}

	public String getPath() {
		return path;
	}

	public void setFilePart(Part filePart) {
		this.filePart = filePart;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public void setWriter(PrintWriter writer) {
		this.writer = writer;
	}

	public void upload(String path, String fileName, Part filePart)
			throws Exception {
		System.out.println("fileName : " + fileName);
		OutputStream out = null;
		File fileSaveDir = new File(path);
		if (!fileSaveDir.exists()) {
			fileSaveDir.mkdir();
		}
		filecontent = filePart.getInputStream();

		int read = 0;
		final byte[] bytes = new byte[1024];
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				filecontent));
		StringBuilder s = new StringBuilder();
		String line;
		while ((line = reader.readLine()) != null) {
			s.append("\n" + line);
		}
		content = s.toString();
		out = new FileOutputStream(path + File.separator + fileName);

		content = removePackage(content);
		System.out.println("Content : " + content);
		byte[] contentInBytes = content.getBytes();

		out.write(contentInBytes);
		out.flush();
		out.close();
	}

	public void upload(String path, String fileName, String code)
			throws Exception {
		System.out.println("fileName : " + fileName);
		OutputStream out = null;
		File fileSaveDir = new File(path);
		if (!fileSaveDir.exists()) {
			fileSaveDir.mkdir();
		}
		filecontent = Tools.StringToInputStream(code);

		int read = 0;
		final byte[] bytes = new byte[1024];
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				filecontent));
		StringBuilder s = new StringBuilder();
		String line;
		while ((line = reader.readLine()) != null) {
			s.append("\n" + line);
		}
		content = s.toString();
		System.out.println("Code : " + code);
		content = code;
		content = removePackage(content);

		out = new FileOutputStream(path + File.separator + fileName);

		System.out.println("Content : " + content);
		byte[] contentInBytes = content.getBytes();

		out.write(contentInBytes);
		out.flush();
		out.close();

	}

	private String removePackage(String content) {
		if (content.contains("package")) {
			int index = content.indexOf(';');
			String stringToRemeve = content.substring(0, index + 1);
			return content.replace(stringToRemeve, "");
		}
		return content;
	}

	public Logger getLOGGER() {
		return LOGGER;
	}

	public Part getFilePart() {
		return filePart;
	}

	public String getFileName() {
		return fileName;
	}

	public PrintWriter getWriter() {
		return writer;
	}

	public String getFileName(Part part) {
		final String partHeader = part.getHeader("content-disposition");
		LOGGER.log(Level.INFO, "Part Header = {0}", partHeader);
		for (String content : part.getHeader("content-disposition").split(";")) {
			if (content.trim().startsWith("filename")) {
				return content.substring(content.indexOf('=') + 1).trim()
						.replace("\"", "");
			}
		}
		return null;
	}

	public String getContent() throws IOException {
		return content;
	}
}